package com.todaynews.ui.home;

import android.view.animation.Animation;
import com.todaynews.R;
import com.todaynews.frame.model.InfoResult;
import com.todaynews.frame.model.MsgBean;
import com.todaynews.frame.ui.activity.BaseActivity;
import com.todaynews.frame.ui.barlibrary.ImmersionBar;
import com.todaynews.frame.ui.fragment.BaseFragment;
import com.todaynews.frame.ui.views.CustomViewPager;
import com.todaynews.frame.ui.views.bottombarLayout.BottomBarItem;
import com.todaynews.frame.ui.views.bottombarLayout.BottomBarLayout;
import com.todaynews.model.event.TabRefreshEvent;
import com.todaynews.ui.home.adapter.HomeAdapter;
import com.todaynews.ui.me.UserFragment;
import com.todaynews.ui.smallvideo.SmallVideoFragment;
import com.todaynews.ui.video.VideoFragment;
import com.todaynews.utils.EventConstants;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import cn.jzvd.Jzvd;

public class HomeActivity extends BaseActivity {
    @BindView(R.id.vp_content)
    CustomViewPager vpContent;
    @BindView(R.id.bottom_bar)
    BottomBarLayout bottomBar;
    private List<BaseFragment> fragments;
    private HomeAdapter homeAdapter;

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .navigationBarEnable(true)
                .navigationBarColor(R.color.white)
                .navigationBarDarkIcon(true)
                .init();
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_home;
    }

    @Override
    public void init() {
        fragments = new ArrayList<>(4);
        fragments.add(new HomeFragment());
        fragments.add(new VideoFragment());
        fragments.add(new SmallVideoFragment());
        fragments.add(new UserFragment());
        homeAdapter = new HomeAdapter(fragments, getSupportFragmentManager());
        vpContent.setAdapter(homeAdapter);
        vpContent.setOffscreenPageLimit(fragments.size());
        bottomBar.setViewPager(vpContent);
        //设置条目点击的监听
        bottomBar.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(BottomBarItem bottomBarItem, int previousPosition, int currentPosition) {
                setStatusBarColor(currentPosition);//设置状态栏颜色
                Jzvd.releaseAllVideos();//底部页签切换或者是下拉刷新，释放资源
                if (currentPosition == 0 || currentPosition == 1) {
                    //如果点击的是首页
                    if (bottomBar.getCurrentItem() == currentPosition) {
                        //如果当前页码和点击的页码一致,进行下拉刷新
                        String channelCode = "";
                        if (currentPosition == 0) {
                            channelCode = ((HomeFragment) fragments.get(0)).getCurrentChannelCode();//获取到首页当前显示的fragment的频道
                        } else {
                            channelCode = ((VideoFragment) fragments.get(1)).getCurrentChannelCode();//获取到视频当前显示的fragment的频道
                        }
                        postTabRefreshEvent(bottomBarItem, currentPosition, channelCode);//发送下拉刷新的事件
                    }
                    return;
                }else {
                    //如果点击了其他条目
                    BottomBarItem bottomItem = bottomBar.getBottomItem(0);
                    bottomItem.setSelectedIcon(R.mipmap.tab_home_selected);//更换为原来的图标
                    cancelTabLoading(bottomItem);//停止旋转动画
                }
            }
        });

    }

    @Override
    public void loadData() {

    }

    @Override
    protected void onSuccess(int what, InfoResult t) {

    }

    @Override
    protected void onFail(int what, InfoResult t) {

    }

    private void setStatusBarColor(int position) {

    }

    private void postTabRefreshEvent(BottomBarItem bottomBarItem, int position, String channelCode) {
        TabRefreshEvent event = new TabRefreshEvent();
        event.setChannelCode(channelCode);
        event.setBottomBarItem(bottomBarItem);
        event.setHomeTab(position == 0);
        MsgBean msgBean = new MsgBean();
        msgBean.setObject(event);
        msgBean.setFlag(EventConstants.tabRefresh);
        getEventBus().post(event);//发送下拉刷新事件
    }

    /**
     * 停止首页页签的旋转动画
     */
    private void cancelTabLoading(BottomBarItem bottomItem) {
        Animation animation = bottomItem.getImageView().getAnimation();
        if (animation != null) {
            animation.cancel();
        }
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }
}
